# @Desc    : 获取数据的工具
# @Author  : meihongliang  
# @Time    : 2025-02-16 12:46
import pandas as pd
import statistics

from pyecharts.charts import Line
from pyecharts import options as opts

from lw_config import config_util


def get_execl_data():
    # 替换为你的Excel文件路径
    file_path = config_util.get_2024_execl()

    # 使用pandas的read_excel函数读取文件
    all_sheet = pd.read_excel(file_path, sheet_name=None, keep_default_na=False)

    def get_w2(w2):
        if w2:
            return w2
        else:
            return 0

    # 日期
    rq = []
    # 体重
    weight = []
    lines = '日期,平均体重\n'
    for sheet_name, sheet_dframe in all_sheet.items():
        # 过滤没有数据的行
        single_df = sheet_dframe.dropna(how="all")

        lines += sheet_name + ":\n"
        for index, row in single_df.iterrows():
            if row['日期']:
                s = []
                if get_w2(row['体重1']) != 0:
                    s.append(get_w2(row['体重1']))
                if get_w2(row['体重2']) != 0:
                    s.append(get_w2(row['体重2']))
                # print(f'{row["日期"]},{s}')
                if len(s) > 0:
                    avg = round(statistics.mean(s), 1)
                    if avg != 0:
                        lines += "\t" + str(row['日期']) + ',' + str(avg) + '\n'
                        rq.append(str(row['日期']))  # x字符串没关系
                        weight.append(avg)  # y轴得要数字了，不然高低不好排
                        # print(f'{row["日期"]},{avg}')

    return rq, weight


def year_weight():
    """年度减肥数据"""
    rq, weight = get_execl_data()
    combined_list = [tuple(pair) for pair in zip(rq, weight)]

    data_max = max(weight)
    data_min = min(weight)
    title = f'最大{data_max}斤,最小{data_min}斤'
    title = '体重变化图'
    #
    line = (
        Line()
        .add_xaxis(rq)  # dates as x-axis
        .add_yaxis(
            "体重(斤)",
            weight,
            markpoint_opts=opts.MarkPointOpts(
                data=[
                    opts.MarkPointItem(type_="max", name="最大值", itemstyle_opts=opts.ItemStyleOpts(color="red")),
                    opts.MarkPointItem(type_="min", name="最小值", itemstyle_opts=opts.ItemStyleOpts(color="red"))
                ],
                symbol="pin",  # 使用更明显的图标来标记
                symbol_size=50  # 标记点的大小
            ))  # values as y-axis
        .set_global_opts(
            yaxis_opts=opts.AxisOpts(
                min_="130",
                max_="175",
            ),
            title_opts=opts.TitleOpts(title=title),
            # xaxis_opts=opts.AxisOpts(type_="time"),
            toolbox_opts=opts.ToolboxOpts(
                is_show=True,
            ),
            tooltip_opts=opts.TooltipOpts(
                trigger="axis",
                axis_pointer_type="cross",
                formatter="{c}"
            ),
            xaxis_opts=opts.AxisOpts(
                min_="dataMin",
                max_="dataMax",
            )
            # datazoom_opts=[
            #     # opts.DataZoomOpts(type_="inside"),
            #     opts.DataZoomOpts(),
            # ]
        )
    )
    return line


def running_heart_rate():
    rq = ['20250204', '20250205', '20250206', '20250207', '20250208', '20250209', '20250210', '20250211', '20250212',
          '20250213', '20250215']
    rate = ['181', '184', '148', '165', '177', '174', '149', '166', '161', '162', '152']
    avg_pace = ['6.56', '6.31', '8.14', '7.12', '6.25', '6.44', '8.07', '6.38', '9.09', '6.23', '6.20']
    distance = ['4.33', '4.60', '3.65', '4.18', '4.69', '4.48', '3.70', '4.59', '3.28', '4.75', '4.79']
    line = (
        Line()
        .add_xaxis(rq)  # dates as x-axis
        .add_yaxis(
            "心率(BPM)",
            rate,
            markpoint_opts=opts.MarkPointOpts(
                data=[
                    opts.MarkPointItem(type_="max", name="最大值", itemstyle_opts=opts.ItemStyleOpts(color="red")),
                    opts.MarkPointItem(type_="min", name="最小值", itemstyle_opts=opts.ItemStyleOpts(color="red"))
                ],
                symbol="pin",  # 使用更明显的图标来标记
                symbol_size=50  # 标记点的大小
            )
        )
        .add_yaxis('每公里配速', avg_pace,
                   markpoint_opts=opts.MarkPointOpts(
                       data=[
                           opts.MarkPointItem(type_="max", name="最大值",
                                              itemstyle_opts=opts.ItemStyleOpts(color="blue")),
                           opts.MarkPointItem(type_="min", name="最小值",
                                              itemstyle_opts=opts.ItemStyleOpts(color="blue"))
                       ],
                       symbol="pin",  # 使用更明显的图标来标记
                       symbol_size=50  # 标记点的大小
                   )
                   )
        .add_yaxis('公里数/30分钟', distance,
                   markpoint_opts=opts.MarkPointOpts(
                       data=[
                           opts.MarkPointItem(type_="max", name="最大值",
                                              itemstyle_opts=opts.ItemStyleOpts(color="green")),
                           opts.MarkPointItem(type_="min", name="最小值",
                                              itemstyle_opts=opts.ItemStyleOpts(color="green"))
                       ],
                       symbol="pin",  # 使用更明显的图标来标记
                       symbol_size=50  # 标记点的大小
                   )
                   )
        .set_global_opts(
            yaxis_opts=opts.AxisOpts(
                min_="dataMin",
                max_="dataMax",
            ),
            title_opts=opts.TitleOpts(title='心率变化图'),
            toolbox_opts=opts.ToolboxOpts(
                is_show=True,
            ),
            tooltip_opts=opts.TooltipOpts(
                trigger="axis",
                axis_pointer_type="cross",
                formatter="{c}"
            ),
            xaxis_opts=opts.AxisOpts(
                min_="dataMin",
                max_="dataMax",
            )
        )
    )
    return line
